package com.study.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.services.ChartService;
import com.study.services.FillSevice;
import com.study.services.IndexService;
import com.study.services.QaService;
import com.study.services.ServiceSerivce;
import com.study.services.TousuService;
import com.study.services.WeixiuService;
import com.study.services.WeixiuTableService;

@Controller
public class BaseController {
	@Autowired
	WeixiuService wservice;// 维修
	@Autowired
	TousuService tservice;// 投诉
	@Autowired
	ServiceSerivce sservice;// 服务
	@Autowired
	ChartService chartservice;// 统计图
	@Autowired
	FillSevice fs;// 销售漏斗
	@Autowired
	IndexService iservice;
	@Autowired
	WeixiuTableService wtservice;//维修总控台
	@Autowired
	QaService qaservice;//QA库

	/**
	 * 进入主主界面
	 * 
	 * @return
	 */
	@GetMapping("/")
	public ModelAndView getForm() {
		ModelAndView mv = new ModelAndView("login");
		System.out.println("我是从主页来的啦啦啦啦啦啦");
		
		return mv;
	}

	/**
	 * 加载主界面
	 */
	@GetMapping("/index")
	public ModelAndView getForms(HttpSession session) {
		ModelAndView mv = new ModelAndView("index_v1");

		mv.addObject("Formchange", chartservice.findByFormchange());
		mv.addObject("CustomerCount", chartservice.findByCustomerCount());
		mv.addObject("OrderMoney", chartservice.findByOrderMoney());
		mv.addObject("OrderState", chartservice.findByOrderState());
		mv.addObject("IndexItem", iservice.returnIndex());
		
		//封装当前用户id
		JSONObject json = new JSONObject();
		String names = (String) session.getAttribute("username");
		int ids =(int) session.getAttribute("useid");
		json.put("name", names);
		json.put("useid",ids);
		JSONArray arry = new JSONArray();
		arry.add(json);
		session.setAttribute("userJson",arry);
		return mv;
	}

	/**
	 * gx 集客
	 */
	@GetMapping("/getCustomergx")
	// @ResponseBody
	public String getForm2(HttpSession session) {
		ModelAndView mv = new ModelAndView("/getCustomer/huokeone");
		System.out.println("我是集客来的。");

		return "redirect:/huokeones";
	}

	/**
	 * ty 获客线索
	 */
	@GetMapping("/getCustomerty")
	@ResponseBody
	public ModelAndView getForm3(HttpSession session) {
		ModelAndView mv = new ModelAndView("/getCustomer/huokexiansuo");
		System.out.println("我是集客来的。");
		return mv;
	}

	/**
	 * wcx 客户
	 */
	@GetMapping("getCustomerwcx")
	@ResponseBody
	public ModelAndView getForm4(HttpSession session) {
		ModelAndView mv = new ModelAndView("/customer/customer");
		return mv;
	}

	/**
	 * wcx 联系人
	 */
	@GetMapping("getLinkmanwcx")
	@ResponseBody
	public ModelAndView getForm5(HttpSession session) {
		ModelAndView mv = new ModelAndView("/customer/linkman");
		return mv;
	}

	/**
	 * lsl 纪念日
	 */
	@GetMapping("getAnniversarylsl")
	@ResponseBody
	public ModelAndView getForm6(HttpSession session) {
		ModelAndView mv = new ModelAndView("/customer/anniversaryList");
		return mv;
	}

	/**
	 * wjl 客户合并
	 */
	@GetMapping("getHebingwjl")
	@ResponseBody
	public ModelAndView getForm7(HttpSession session) {
		ModelAndView mv = new ModelAndView("/customer/kehuhebing");
		return mv;
	}

	/**
	 * wjl 客户转移
	 */
	@GetMapping("getZhuanyiwjl")
	@ResponseBody
	public ModelAndView getForm8(HttpSession session) {
		ModelAndView mv = new ModelAndView("/customer/piliangchognzhi");
		return mv;
	}

	/**
	 * wcx 销售机会
	 */
	@GetMapping("/getJihuiwcx")
	@ResponseBody
	public ModelAndView getForm9(HttpSession session) {
		ModelAndView mv = new ModelAndView("/beforeSale/opport");
		return mv;
	}

	/**
	 * wcx 销售漏斗
	 */
	@GetMapping("/getLouduwcx")
	@ResponseBody
	public ModelAndView getForm10(HttpSession session,HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/beforeSale/fill");
		String useid =request.getSession().getAttribute("useid").toString();
		JSONObject selectFill = fs.selectFill(useid);
		System.out.println("来了老弟fill");
		mv.addObject("fill", selectFill);
		return mv;
	}

	/**
	 * wcx 客户需求
	 */
	@GetMapping("/getXuqiuwcx")
	@ResponseBody
	public ModelAndView getForm11(HttpSession session) {
		ModelAndView mv = new ModelAndView("/beforeSale/demand");
		return mv;
	}

	/**
	 * wcx 解决方案
	 */
	@GetMapping("/getFanganwcx")
	@ResponseBody
	public ModelAndView getForm12(HttpSession session) {
		ModelAndView mv = new ModelAndView("/beforeSale/solution");
		return mv;
	}

	/**
	 * wcx 竞争对手
	 */
	@GetMapping("/getDuiShouwcx")
	@ResponseBody
	public ModelAndView getForm13(HttpSession session) {
		ModelAndView mv = new ModelAndView("/beforeSale/competitors");
		return mv;
	}

	/**
	 * wcx 报价单
	 */
	@GetMapping("/getBaojiawcx")
	@ResponseBody
	public ModelAndView getForm14(HttpSession session) {
		ModelAndView mv = new ModelAndView("/beforeSale/price");
		return mv;
	}

	/**
	 * wcx 报价明细
	 */
	@GetMapping("/getBaojiaMorewcx")
	@ResponseBody
	public ModelAndView getForm15(HttpSession session) {
		ModelAndView mv = new ModelAndView("beforeSale/pricedetail");
		return mv;
	}

	/**
	 * gx 订单
	 */
	@GetMapping("/getOrdergx")
	// @ResponseBody
	public ModelAndView getForm16(HttpSession session) {
		ModelAndView mv = new ModelAndView("/onSale/createOrder");
		mv.addObject("OrderMoney", chartservice.findByOrderMoney());
		mv.addObject("OrderState", chartservice.findByOrderState());
		return mv;
	}

	/**
	 * gx 订单明细
	 */
	@GetMapping("/getOrderMoregx")
	@ResponseBody
	public ModelAndView getForm17(HttpSession session) {
		ModelAndView mv = new ModelAndView("/onSale/goods");
		return mv;
	}

	/**
	 * gx 发货
	 */
	@GetMapping("/getFahuogx")
	@ResponseBody
	public ModelAndView getForm18(HttpSession session) {
		ModelAndView mv = new ModelAndView("/onSale/delinote");
		return mv;
	}

	/**
	 * gx 退货
	 */
	@GetMapping("/getBackgx")
	@ResponseBody
	public ModelAndView getForm19(HttpSession session) {
		ModelAndView mv = new ModelAndView("/onSale/returnGoods");
		return mv;
	}

	/**
	 * gx 退货明细
	 */
/*	@GetMapping("/getBackMoregx")
	@ResponseBody
	public ModelAndView getForm20(HttpSession session) {
		ModelAndView mv = new ModelAndView("/onSale/");
		return mv;
	}*/

	/**
	 * ty 售后服务
	 */
	@GetMapping("/getServicety")
	@ResponseBody
	public ModelAndView getForm21(HttpSession session,HttpServletRequest request) {
		String cusid = request.getParameter("cusid");
		ModelAndView mv = new ModelAndView("/afterSale/kehufuwu");
		List<JSONObject> list = sservice.selectBySel(1, 6, "", "",cusid);
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		mv.addObject("servicetys", json);
		mv.addObject("servicemode", sservice.selectByMode());
		mv.addObject("servicecount", sservice.selectByCount());
		return mv;
	}

	/**
	 * ty 售后投诉
	 */
	@GetMapping("/getTousuty")
	@ResponseBody
	public ModelAndView getForm22(HttpSession session) {
		ModelAndView mv = new ModelAndView("/afterSale/kehutousu");
		mv.addObject("tousucalzz", tservice.selectByclazz());
		mv.addObject("tousuresult", tservice.selectByResult());
		return mv;
	}

	/**
	 * ty 客户关怀
	 */
	@GetMapping("/getCarety")
	@ResponseBody
	public ModelAndView getForm23(HttpSession session) {
		ModelAndView mv = new ModelAndView("/afterSale/kehuguanhuai");
		return mv;
	}

	/**
	 * ty 维修
	 */
	@GetMapping("/getWeixiuty")
	@ResponseBody
	public ModelAndView getForm24(HttpSession session) {
		ModelAndView mv = new ModelAndView("/afterSale/kehuweixiu");
		mv.addObject("weixiuresult", wservice.selectByResult());
		mv.addObject("weixiuresult1", wservice.selectByResult1());
		return mv;
	}

	/**
	 * lsl 产品分类
	 */
	@GetMapping("getCatlsl")
	@ResponseBody
	public ModelAndView getForm25(HttpSession session) {
		ModelAndView mv = new ModelAndView("/goods/category");
		return mv;
	}

	/**
	 * lsl 产品
	 */
	@GetMapping("getGoodlsl")
	@ResponseBody
	public ModelAndView getForm26(HttpSession session) {
		ModelAndView mv = new ModelAndView("/goods/productList");
		return mv;
	}

	/**
	 * ty 统计
	 */
	@GetMapping("getChartty")
	public ModelAndView getForm28(HttpSession session) {
		ModelAndView mv = new ModelAndView("echarts");
		mv.addObject("Formchange", chartservice.findByFormchange());
		mv.addObject("CustomerCount", chartservice.findByCustomerCount());
		mv.addObject("OrderMoney", chartservice.findByOrderMoney());
		mv.addObject("OrderState", chartservice.findByOrderState());
		return mv;
	}

	
	
	/**
	 * ty 服务器错误
	 */
	@GetMapping("getErrorty")
	public ModelAndView getForm29(HttpSession session) {
		ModelAndView mv = new ModelAndView("/500");
		return mv;
	}

	/**
	 * 错误界面返回主界面
	 */
	@GetMapping("/setErrorty")
	public ModelAndView getForm30(HttpSession session) {
		ModelAndView mv = new ModelAndView("/index");
		return mv;
	}
	
	/**
	 * ty
	 * 维修总控台
	 */
	@GetMapping("/getWeixiuTablety")
	public ModelAndView getForm31(HttpSession session) {
		ModelAndView mv ;
		Integer userid = (Integer) session.getAttribute("useid");
		if ("".equals(userid) ) {
			mv = new ModelAndView("");
		}else {
			mv = new ModelAndView("/afterSale/weixiuTable");
		}
		List<JSONObject> object= wtservice.queryAll(new Date(),464,1,8,userid+"");
		System.out.println(object);
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(object);
		String json = JSONObject.toJSONString(pager);
		mv.addObject("weixiuTableBy", json);
		return mv;
	}
	

	/**
	 * ty 客服总控台
	 */
	@GetMapping("/getServiceTablety")
	public ModelAndView getForm36(HttpSession session) {
		ModelAndView mv = new ModelAndView("/afterSale/serviceTable");
		//mv.addObject("weixiuresult", wservice.selectByResult());
		return mv;
	}
	
	
	/**
	 * lsl 库存列表
	 */
	@GetMapping("/deportitem")
	@ResponseBody
	public ModelAndView getForm32(HttpSession session) {
		ModelAndView mv = new ModelAndView("depot/deportitem");
		return mv;
	}
	
	/**
	 * lsl 入库
	 */
	@GetMapping("/instockes")
	@ResponseBody
	public ModelAndView getForm33(HttpSession session) {
		ModelAndView mv = new ModelAndView("depot/instockes");
		mv.addObject("shelfs", -1);
		return mv;
	}
	
	

	/**
	 * lsl 出库
	 */
	@GetMapping("/treasury")
	@ResponseBody
	public ModelAndView getForm34(HttpSession session) {
		ModelAndView mv = new ModelAndView("depot/treasury");
		return mv;
	}	
	
	/**
	 * lsl 出入库明细
	 */
	@GetMapping("inoutlife")
	@ResponseBody
	public ModelAndView getForm35(HttpSession session) {
		ModelAndView mv = new ModelAndView("depot/inoutlife");
		return mv;
	}
	/**
	 * ty QA库
	 */
	@GetMapping("/getQAty")
	public ModelAndView getForm38(HttpSession session) {
		ModelAndView mv = new ModelAndView("/afterSale/kehuQa");
		List<JSONObject> list = qaservice.selectBySel(1, 6, "", "");
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		mv.addObject("Qaty", json);
		return mv;
	}
	
	/**
	 * 访问地址http://localhost:8089/createOrder
	 * 访问制作获客html5
	 * @return
	 *
	 */
	@RequestMapping("/dohtml5")
	public String getForm39(HttpSession session) {
		// List<Orderform> list = service.selectByPager(1, 8);
		// session.setAttribute("DICTIONARY43", list);
		return "getCustomer/dohtml5";
	}

}
